'''
Created on Jul 12, 2011

@author: kafkef
'''

from PyQt4.QtGui import *
from PyQt4.QtCore import QObject, SIGNAL, pyqtSlot, SLOT, QDir

from dexen.system.client import ui
from dexen.system.common.excepts import DexenInvalidJobDefError,\
    DexenInvalidJobNameError


svr_proxy = ui.svr_proxy


class NewJobDialog(QDialog):
    def __init__(self, parent):
        QDialog.__init__(self, parent)
        self.setWindowTitle("Run New Job")
        self.setFixedHeight(150)
        self.setFixedWidth(500)
        self.setModal(True)
        self._create_widgets()        
       
    def _create_widgets(self):
        # creating widget objects
        self._job_name_edit = QLineEdit()
        self._job_def_path_edit = QLineEdit()
        self._browse_button = QPushButton("Browse")
        self._button_box = QDialogButtonBox(QDialogButtonBox.Ok | 
                                            QDialogButtonBox.Cancel)
        
        self._job_def_path_edit.setReadOnly(True)
        
        # laying out child widgets
        v_layout = QVBoxLayout()
        
        h_layout = QHBoxLayout()
        h_layout.addWidget(self._job_def_path_edit)
        h_layout.addWidget(self._browse_button)
         
        form_layout = QFormLayout()
        form_layout.addRow("Job Name:", self._job_name_edit)
        form_layout.addRow("JobDef Dir:", h_layout)
        
        v_layout.addLayout(form_layout)
        v_layout.addWidget(self._button_box)
        
        self.setLayout(v_layout)
        
        # connecting signal/slots
        QObject.connect(self._button_box, SIGNAL("accepted()"),
                        self, SLOT("accept()"))
        QObject.connect(self._button_box, SIGNAL("rejected()"),
                        self, SLOT("reject()"))
        QObject.connect(self._browse_button, SIGNAL("clicked()"),
                        self.browse_button_clicked)
    
    def browse_button_clicked(self):
        print "browse button is clicked"
        dir = QFileDialog.getExistingDirectory(self, "Open Directory", 
                                               QDir.rootPath(),
                                               QFileDialog.ShowDirsOnly | 
                                               QFileDialog.DontResolveSymlinks)
        self._job_def_path_edit.setText(dir)
    
    @pyqtSlot()
    def accept(self):
        print "I am in accept slot"
        job_name = str(self._job_name_edit.text())
        job_def_dir = str(self._job_def_path_edit.text())
        try:
            svr_proxy.start_job(job_name, job_def_dir)
        except DexenInvalidJobDefError:
            err_msg = "The specified folder is not a valid job def folder.\n"
            err_msg = "Please select a valid job def folder."
            msg_box = QMessageBox(self)
            msg_box.setText(err_msg)
            msg_box.exec_()
        except DexenInvalidJobNameError, err:
            msg_box = QMessageBox(self)
            msg_box.setText(err.message)
            msg_box.exec_()            
        else:
            QDialog.accept(self)
    
    
    